package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.EmploymentContract;
import com.ruoyi.system.mapper.EmploymentContractMapper;
import com.ruoyi.system.service.IEmploymentContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class EmploymentContractServiceImpl implements IEmploymentContractService {

    @Autowired
    private EmploymentContractMapper employmentContractMapper;

    @Override
    public List<EmploymentContract> selectEmploymentContractList(EmploymentContract EmploymentContract) {
        return employmentContractMapper.selectEmploymentContractList(EmploymentContract);
    }

    @Override
    public EmploymentContract getEmploymentContractById(String contractId) {
        return employmentContractMapper.getEmploymentContractById(contractId);
    }

    @Override
    public int insertEmploymentContract(EmploymentContract employmentContract) {
        String contractId = UUID.randomUUID().toString().replaceAll("-","");
        employmentContract.setEcId(contractId);
        return employmentContractMapper.insertEmploymentContract(employmentContract);
    }

    @Override
    public int updateEmploymentContract(EmploymentContract employmentContract) {
        return employmentContractMapper.updateEmploymentContract(employmentContract);
    }

    @Override
    public int removeEmploymentContract(String ids) {
        return employmentContractMapper.removeEmploymentContract(Convert.toStrArray(ids));
    }

    @Override
    public String importEmploymentContract(List<EmploymentContract> ecList) {
        //校验 状态 类型 日期
        int errorData = 0;
        int updateCount = 0;
        int insertCount = 0;
        StringBuilder errorDatas = new StringBuilder();
        for(EmploymentContract ec:ecList){
            if(null==ec.getNum()){
                errorData++;
                String errStr = "合同" + ec.getNum()+":"+ec.getName() + "序号为空；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(!"1".equals(ec.getStatus())&&!"2".equals(ec.getStatus())&&!"3".equals(ec.getStatus())){
                errorData++;
                String errStr = "合同" + ec.getNum()+":"+ec.getName() + "状态异常；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(!"1".equals(ec.getContractType())&&!"2".equals(ec.getContractType())&&!"3".equals(ec.getContractType())){
                errorData++;
                String errStr = "合同" + ec.getNum()+":"+ec.getName() + "类型错误；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if(StringUtils.isNotEmpty(ec.getEcId())){  //update
                try{
                    EmploymentContract propertyContract = employmentContractMapper.getEmploymentContractById(ec.getEcId());
                    if(propertyContract!=null){
                        employmentContractMapper.updateEmploymentContract(ec);
                    }else{
                        employmentContractMapper.insertEmploymentContract(ec);
                    }
                    updateCount++;
                }catch (Exception e){
                    e.printStackTrace();
                    errorData++;
                    String errStr = "合同" + ec.getNum()+":"+ec.getName() + "修改失败；"+e.getMessage()+"</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            }else{  //insert
                try{
                    ec.setEcId(UUID.randomUUID().toString().replaceAll("-",""));
                    employmentContractMapper.insertEmploymentContract(ec);
                    insertCount++;
                }catch (Exception e){
                    e.printStackTrace();
                    errorData++;
                    String errStr = "合同" + ec.getNum()+":"+ec.getName() + "新增失败；"+e.getMessage()+"</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            }
        }
        String message;
        if(ecList.size()>0){
            message = "共导入"+ecList.size()+"条数据。</br>";
        }else{
            return "无数据";
        }
        message = message + "成功新增" + insertCount + "条数据，成功更新"+updateCount+"条数据</br>";
        if(StringUtils.isNotEmpty(errorDatas.toString())){
            message = message + "其中" + errorData + "条数据导入失败，错误原因如下：</br>";
            message = message + errorDatas.toString();
        }
        return message;
    }
}
